package com.huiyujiaoyu.powertime.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.AppCompatRatingBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import com.huiyujiaoyu.base.BaseActivity;
import com.huiyujiaoyu.http.HttpApi;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.IndexItemModel;
import com.huiyujiaoyu.powertime.model.SimpleListBean;
import com.huiyujiaoyu.util.ActivityUtil;
import com.huiyujiaoyu.util.Constant;
import com.huiyujiaoyu.util.GlideUtil;
import com.huiyujiaoyu.util.StringUtils;
import com.huiyujiaoyu.widget.CustomLoadMoreView;
import com.huiyujiaoyu.widget.LlkjToolBar;


import java.util.ArrayList;
import java.util.List;


import butterknife.BindView;


/**
 * Created by hhwly on 2017-06-30.
 */

public class CommentActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.recycle)
    RecyclerView mRvTwo;
    @BindView(R.id.swipe)
    SwipeRefreshLayout mSwipe;
    @BindView(R.id.toolbar)
    LlkjToolBar mToolbar;
    private CustomLoadMoreView loadmore;
    public static void launch(Context context, String id,int pid,int column_id) {
        Bundle bundle = new Bundle();
        bundle.putString(Constant.DATA, id);
        bundle.putInt(Constant.DATA2, pid);
        bundle.putInt(Constant.DATA3, column_id);
        ActivityUtil.startActivity(context, CommentActivity.class, bundle);
    }
    private BaseQuickAdapter<IndexItemModel,BaseViewHolder> adapter;
    private List<IndexItemModel> pls;
    private int page = 1;
    private int pid,column_id;// 头条新闻内容的id
    private String id;

    public void initView() {
        setToolbar(mToolbar);
        mToolbar.setToolBar("所有评论", true, "", R.mipmap.icon_titlebar_back, false, "", LlkjToolBar.isNull);
        id = getIntent().getStringExtra(Constant.DATA);
        pid = getIntent().getIntExtra(Constant.DATA2, 0);
        column_id = getIntent().getIntExtra(Constant.DATA3, 0);
        mRvTwo.setLayoutManager(new LinearLayoutManager(this));
        mSwipe .setColorSchemeColors(getResources().getColor(R.color.blue_));
        mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

    public void initData() {
        pls = new ArrayList<>();
        adapter = new BaseQuickAdapter<IndexItemModel,BaseViewHolder>(R.layout.comment_list, pls) {
            @Override
            protected void convert(BaseViewHolder helper, IndexItemModel model) {
                GlideUtil.loadCircleImg((ImageView) helper.getView(R.id.cv_head), StringUtils.getFullUrl(model.photo));
                helper.setText(R.id.tv_name, model.name);
                helper .setText(R.id.tv_time, model.created_at);
                helper .setText(R.id.tv_desc, model.content);
                AppCompatRatingBar rb = helper.getView(R.id.ratbar);
                rb.setRating(model.score);
            }
        };
        loadmore = new CustomLoadMoreView();
        adapter.setLoadMoreView(loadmore);
        mRvTwo.setAdapter(adapter);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                 loadMore();
            }
        },mRvTwo);
        refresh();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_titswirecy;
    }

    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HttpApi.HTTP_COMMENT_INDEX:
                doData(((SimpleListBean) o).list);
                break;
        }
    }


    public void getData(){
        HttpMethods.getInstance().cIndex(mContext,getComp(),this,id,column_id+"",pid+"",page);
    }
    private void refresh(){
        if(mContext==null)
            return;
        if(adapter==null){
            return;
        }
        if(adapter.isLoadMoreEnable()) {
            adapter.setEnableLoadMore(false);
        }
        page = 1;
        getData();
    }
    private void loadMore(){
        if(mSwipe.isEnabled()){
            mSwipe.setEnabled(false);
        }
        page++;
        getData();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ibtn_comment:
                break;
        }
    }

    public void doData(List<IndexItemModel> data){
        if (page == 1) {
            pls.clear();
            // 设置加载更多能用
            if (mSwipe.isRefreshing()) {
                mSwipe.setRefreshing(false);
            }
            pls.addAll(data);
            adapter.notifyDataSetChanged();
            adapter.setEnableLoadMore(true);
            Log.e(TAG,"page == 1");
        }else{
            if (data == null||data.size() ==0 ) {
                adapter.loadMoreEnd();
            }else{
                pls.addAll(data);
                adapter.notifyDataSetChanged();
                adapter.loadMoreComplete();
            }
            if(!mSwipe.isEnabled())
                mSwipe.setEnabled(true);
            Log.e(TAG,"page != 1");
        }
        Log.e(TAG,"page=="+page);
    }
}
